package com.ws.shop.entity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.ws.shop.util.StringUtil;


public class MailAuth {

	public final static String TABLE_NAME = "mail_auth";
	
	public final static int LOGIN_PASSWORD_RECOVER_KEY = 1;//登陆密码找回
	public final static int SECOND_PASSWORD_RECOVER_KEY = 2;//二级密码找回
	public final static int PAY_PWD_RECOVER_KEY = 3;//支付密码找回
	public final static int LOGIN_PASSWORD_MODIFY_KEY = 4;//登陆密码修改
	public final static int SECOND_PASSWORD_MODIFY_KEY = 5;//二级密码修改
	public final static int PAY_PWD_MODIFY_KEY = 6;//支付密码修改
	public final static int ASSIGN_ACCOUNT_KEY = 7;//账号转让随机码
	public final static int INIT_PASSWORD_KEY = 8;//初始随机密码
	public final static int PHONE_BIND_KEY = 9;//手机号绑定
	public final static int PHONE_LOGIN_KEY = 10;//手机验证码登录
	public final static int PHONE_REBIND_KEY = 11;//重新绑定手机对旧号码验证
	public final static int PHONE_REBIND_NEW_KEY = 11;//重新绑定手机对新号码验证
	
	
	public final static Map<Integer,String> ALL_AUTH_TYPE = new HashMap<Integer,String>();
	
	private final static Map<Integer,String> VALID_MESSAGES = new HashMap<Integer,String>();
	
	static{
		ALL_AUTH_TYPE.put(LOGIN_PASSWORD_RECOVER_KEY, "登陆密码找回");
		ALL_AUTH_TYPE.put(SECOND_PASSWORD_RECOVER_KEY, "二级密码找回");
		ALL_AUTH_TYPE.put(PAY_PWD_RECOVER_KEY, "支付密码找回");
		ALL_AUTH_TYPE.put(LOGIN_PASSWORD_MODIFY_KEY, "登陆密码修改");
		ALL_AUTH_TYPE.put(SECOND_PASSWORD_MODIFY_KEY, "二级密码修改");
		ALL_AUTH_TYPE.put(PAY_PWD_MODIFY_KEY, "支付密码修改");
		ALL_AUTH_TYPE.put(ASSIGN_ACCOUNT_KEY, "账号转让随机码");
		ALL_AUTH_TYPE.put(INIT_PASSWORD_KEY, "初始随机密码");
		
		VALID_MESSAGES.put(1, "您还未发送验证码，请重新发送");
		VALID_MESSAGES.put(2, "手机验证密码已过时,请重新发送");
		VALID_MESSAGES.put(3, "手机验证密码错误,请仔细核对");
	}
	
	public static String getValidMessage(int result){
		String msg = VALID_MESSAGES.get(result);
		if(msg == null)
			msg = "系统错误，请稍后再试";
		return msg;
	}
	
	public static final int LOGIN_PASSWORD_RECOVER_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	public static final int SECOND_PASSWORD_RECOVER_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	public static final int PAY_PWD_RECOVER_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	public static final int LOGIN_PASSWORD_MODIFY_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	public static final int SECOND_PASSWORD_MODIFY_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	public static final int PAY_PWD_MODIFY_KEY_PERIOD = 3600000;// 密码找回Key有效时间（单位：毫秒,默认1小时）
	
	public static final int PHONE_BIND_KEY_PERIOD = 300000;//有效时间  默认5分钟
	public static final int PHONE_LOGIN_KEY_PERIOD = 300000;//有效时间  默认5分钟
	public final static int PHONE_REBIND_KEY_PERIOD = 3600000;//重新绑定手机号码验证码有效时间 默认1小时
	public final static int PHONE_REBIND_NEW_KEY_PERIOD = 3600000;//重新绑定手机号码验证码有效时间 默认1小时
	
	public static final int AUTH_CODE_LENGTH = 8;
	
	private long id;
	private long memberid;
	private String auth_code;
	private int auth_type;
	private Date createtime;
	
	public MailAuth(){}
	
	public MailAuth(long memberid,int auth_type,String auth_code){
		this.memberid = memberid;
		this.auth_type = auth_type;
		this.auth_code = auth_code;
	}
	
	public static boolean validMailType(int auth_type){
		String str = ALL_AUTH_TYPE.get(auth_type);
		return str == null?false:true;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getMemberid() {
		return memberid;
	}
	
	public void setMemberid(long memberid) {
		this.memberid = memberid;
	}

	public String getAuth_code() {
		return auth_code;
	}
	public void setAuth_code(String authCode) {
		auth_code = authCode;
	}
	public int getAuth_type() {
		return auth_type;
	}
	public void setAuth_type(int authType) {
		auth_type = authType;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	
	public static String validAuthCode(String auth_code){
		if(StringUtil.isEmpty(auth_code)){
			return "邮箱验证码格式错误";
		}
		if(auth_code.length() != AUTH_CODE_LENGTH){
			return "邮箱验证码格式错误,长度应为"+AUTH_CODE_LENGTH;
		}
		return null;
	}
	
	private static String PHONE_VALID_REG = "\\d{6}";
	public static String validPhoneCode(String phoneCode){
		if(StringUtil.isEmpty(phoneCode)){
			return "手机验证码不能为空";
		}
		if(!phoneCode.matches(PHONE_VALID_REG)){
			return "手机验证码必须为6位数字";
		}
		return null;
	}
}
